package de.hwrberlin.remo.diagram.properties;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import de.hwrberlin.remo.model.RemoObject;
import de.hwrberlin.remo.model.LinkObject;

public class NameSection extends AbstractRemoPropertySection {
	
	private Text textName;
	private Label labelName;

	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		
		Composite sectionClient = getWidgetFactory().createFlatFormComposite(parent);
		sectionClient.setLayout(new FormLayout());
		
		labelName = getWidgetFactory().createLabel(sectionClient, "Name:");
		
		FormData data = new FormData();
		data.width = SectionConstants.LABEL_WIDTH;
		data.top   = new FormAttachment(0, SectionConstants.TOP_MARGIN);
		data.left  = new FormAttachment(0, SectionConstants.LABEL_INDENT);
		labelName.setLayoutData(data);

		// ------------------------------------------------------------
		
		textName  = getWidgetFactory().createText(sectionClient, "", SWT.SINGLE | SWT.BORDER);
		textName.addKeyListener(nameKeyListener);
		textName.addFocusListener(nameFocusListener);
		
		data = new FormData();
		data.left  = new FormAttachment(labelName);
		data.right = new FormAttachment(55);
		textName.setLayoutData(data);
	}
	
	@Override
	public void refresh() {
		EObject eObject = getEObject();
	
		if(eObject != null && eObject instanceof RemoObject) {
			RemoObject exObject = (RemoObject) eObject;
			
			if(exObject.getName() != null) {
				textName.setText(exObject.getName());
			} else {
				textName.setText("");
			}
		} 
		
		if(eObject != null && eObject instanceof LinkObject) {
			LinkObject exObject = (LinkObject) eObject;
			
			if(exObject.getLinkname() != null) {
				textName.setText(exObject.getLinkname());
			} else {
				textName.setText("");
			}
		} 
	}
	
	//////////////
	/* Listener */
	
	private KeyListener nameKeyListener = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			if((e.character == 10 || e.character == 13) && 
					(getEObject() != null && getEObject() instanceof RemoObject)) {
				editingHelper.setObjectName((RemoObject) getEObject(), 
						textName.getText());	
			}
			
			if((e.character == 10 || e.character == 13) &&
					(getEObject() != null && getEObject() instanceof LinkObject)) {
				editingHelper.setObjectName((LinkObject) getEObject(), 
						textName.getText());	
			}
		}
	};
	
	private FocusListener nameFocusListener = new FocusAdapter() {
		@Override
		public void focusLost(FocusEvent e) {
			if(eObject != null && eObject instanceof RemoObject) {
			editingHelper.setObjectName((RemoObject) getEObject(), 
					textName.getText());
			}
			if(eObject != null && eObject instanceof LinkObject) {
			editingHelper.setObjectName((LinkObject) getEObject(), 
					textName.getText());
			}
		}	
	};
}